<?php
namespace libraries\sms;

use libraries\Sms;

/**
 * 
 * @author fzq
 * @date 2016-09-08
 */
trait TJuHeSms
{
	/**
	 * array (size=3)
  'reason' => string '错误的手机号码' (length=21)
  'result' => null
  'error_code' => int 205401
  
  
  array (size=3)
  'reason' => string '操作成功' (length=12)
  'result' => 
    array (size=3)
      'sid' => string '1000908161440364600' (length=19)
      'fee' => int 1
      'count' => int 1
  'error_code' => int 0
  */
  
	/**
	 * 请求接口返回内容
	 * @param  string $url [请求的URL地址]
	 * @param  string $params [请求的参数]
	 * @param  int $ipost [是否采用POST形式]
	 * @return  string
	 */
	private function sendSmsJuHe( $arrParams = false, $isPost = 1 )
	{
		$smsConf = array(
				'key'   => $this->_defaultSmsCfg->key,//'13daecb1ca58da68daa5f9dd8fc5feeb', //您申请的APPKEY
				'mobile'    => $arrParams['mobile'], //接受短信的用户手机号码
				'tpl_id'    => $this->_defaultSmsCfg->{'tpl_' . $arrParams[ 'tplName' ] }->id, //您申请的短信模板ID，根据实际情况修改
		);
		
		$tpl_value = '';
		foreach( $arrParams[ 'params' ] as $k => $v )
		{
			$tpl_value .= "#$k#=" . $v . '&';
		}
		
		$smsConf[ 'tpl_value' ] = $tpl_value;
		
		$strUrl = $this->_defaultSmsCfg->url;
		
		$httpInfo = array();
		$ch = curl_init();
		curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
		curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
		curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
		curl_setopt( $ch, CURLOPT_TIMEOUT, 30);
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
	
		if( $isPost )
		{
			curl_setopt( $ch, CURLOPT_POST, true );
			curl_setopt( $ch, CURLOPT_POSTFIELDS, $smsConf );
			curl_setopt( $ch, CURLOPT_URL, $strUrl );
		}
		else
		{
			if( $smsConf )
			{
				curl_setopt( $ch, CURLOPT_URL, $strUrl.'?'.$smsConf );
			}
			else
			{
				curl_setopt( $ch, CURLOPT_URL, $strUrl );
			}
		}
	
		header('content-type:text/html;charset=utf-8');
		$response = curl_exec( $ch );
		if( $response === FALSE )
		{
			//echo "cURL Error: " . curl_error($ch);
			return false;
		}
	
		$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
		$httpInfo = array_merge( $httpInfo, curl_getinfo( $ch ) );
		curl_close( $ch );
		return $response;
	}

}
